package com.trace.pplos.load.common;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.qcloud.cos.COSClient;
import com.qcloud.cos.ClientConfig;
import com.qcloud.cos.request.DelFileRequest;
import com.qcloud.cos.request.GetFileLocalRequest;
import com.qcloud.cos.request.ListFolderRequest;
import com.qcloud.cos.sign.Credentials;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * Project Name:pplos
 * Package Name:com.trace.pplos.load.common
 *
 * @author：zhouqilin
 * @date：2017/8/6 10:55
 * Copyright (c) 2017,jv.com.cn All Rights Reserved.
 */
public class CosUtil {

    private final static Logger logger = LoggerFactory.getLogger(CosUtil.class);

    volatile private static COSClient cosClient = null;

    volatile private static CosUtil cosUtil = null;

    private CosUtil() {
    }

    public static CosUtil getCosUtil() {
        try {
            if (cosUtil == null) {
                Thread.sleep(10);
                synchronized (CosUtil.class) {
                    if (cosUtil == null) {
                        cosUtil = new CosUtil();
                        Credentials credentials = new Credentials(Constant.appId, Constant.secretId, Constant.secretKey);
                        ClientConfig clientConfig = new ClientConfig();
                        clientConfig.setRegion(Constant.region);
                        cosClient = new COSClient(clientConfig, credentials);
                    }
                }
            }
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
        }
        return cosUtil;
    }

    public boolean download(String sourcePath, String targetPath) {
        GetFileLocalRequest getFileLocalRequest =
                new GetFileLocalRequest(Constant.bucketName, sourcePath, targetPath);
        getFileLocalRequest.setUseCDN(false);
        getFileLocalRequest.setReferer(Constant.referer);
        String result = cosClient.getFileLocal(getFileLocalRequest);
        com.alibaba.fastjson.JSONObject object = JSON.parseObject(result);
        logger.debug("download，sourcePath：{}，targetPath：{}，result：{}", sourcePath, targetPath, object);
        return (object.containsKey("code") && object.getInteger("code") == 0);
    }

    public JSONArray listFolder() {
        String cosPath = Constant.getRootPath();
        ListFolderRequest listFolderRequest = new ListFolderRequest(Constant.bucketName, cosPath);
        String result = cosClient.listFolder(listFolderRequest);
        com.alibaba.fastjson.JSONObject object = JSON.parseObject(result);
        logger.debug("listFolder，result：{}", object);
        if (object.containsKey("code") && object.getInteger("code") == 0) {
            return object.getJSONObject("data").getJSONArray("infos");
        } else {
            return new JSONArray();
        }
    }

    public boolean remove(String targetPath) {
        DelFileRequest delFileRequest = new DelFileRequest(Constant.bucketName, targetPath);
        String result = cosClient.delFile(delFileRequest);
        com.alibaba.fastjson.JSONObject object = JSON.parseObject(result);
        logger.debug("remove，targetPath：{}，result：{}", targetPath, object);
        return (object.containsKey("code") && object.getInteger("code") == 0);
    }

}
